export const getURLhandler = (url) => {
  let list = url.split("/");
  let strs = list[list.length - 1].split("&");
  // console.log(strs);
  let obj = {};
  strs.map((item) => {
    let arr = item.split("=");
    obj[arr[0]] = arr[1];
  });
  return obj;
};

export const fileTypeMap = {
  xls: "application/vnd.ms-excel",
  xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  doc: "application/msword",
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  pdf: "application/pdf",
  ppt: "application/ppt",
  pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  png: "image/png",
  gif: "image/gif",
  jpeg: "image/jpeg",
  jpg: "image/jpeg",
  txt: "text/plain",
};

export function exportToFile(data, fileName) {
  const type = fileName.substring(fileName.lastIndexOf(".") + 1);
  let blob = new Blob([data], { type }); // 设置文件类型excel
  let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
  // 创建url之后可以模拟对此文件对象的一系列操作，例如：预览、下载
  let a = document.createElement("a");
  a.href = url;
  a.download = fileName;
  a.click();
  // 释放这个临时的对象url
  window.URL.revokeObjectURL(url);
}

export function exportToExcel(data, name, type = "application/vnd.ms-excel") {
  let blob = new Blob([data], { type }); // 设置文件类型excel
  let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
  // 创建url之后可以模拟对此文件对象的一系列操作，例如：预览、下载
  let a = document.createElement("a");
  a.href = url;
  a.download = name + ".xlsx";
  a.click();
  // 释放这个临时的对象url
  window.URL.revokeObjectURL(url);
}

/**日期 */
export function getCurentDate() {
  const currentDate = new Date();

  const curDay = currentDate.getDate(); //当前号
  const curMonth = currentDate.getMonth() + 1; //当前月
  const curYear = currentDate.getFullYear(); //当前年

  return {
    curDay,
    curMonth,
    curYear,
  };
}

export function endsWithAny(str, suffixes) {
  return suffixes.some(function (suffix) {
    return str.endsWith(suffix);
  });
}
export const toOptions = (enums) => {
  return Object.entries(enums).map((item) => ({
    label: item[1],
    value: item[0],
  }));
};

export function formatNumber(num, chart = ",", length = 3) {
  let result = "";
  let nums = num?.toString().split(".");
  let int = nums[0];
  let decmial = nums[1] ? "." + nums[1] : "";
  let index = 0;
  for (let n = int.length - 1; n >= 0; n--) {
    index++;
    result = int[n] + result;
    if (index % length === 0 && n !== 0) {
      result = chart + result;
    }
  }
  return result + decmial;
}

export const toPercent = (dividend, deno, length = 2) => {
  if (deno == 0) return "-";
  return ((dividend * 100) / deno).toFixed(length) + "%";
};

//转千分位
export function transformNumber(number) {
  if (!number && number !== 0) return "-";
  // 将数字转换为字符串
  let numStr = number?.toString();

  // 分离符号、整数部分和小数部分
  let sign = numStr.startsWith("-") ? "-" : "";
  let parts = numStr.replace(/^-/, "").split(".");
  let integerPart = parts[0];
  let decimalPart = parts.length > 1 ? "." + parts[1] : "";

  // 添加千位分隔符到整数部分
  integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");

  // 返回符号、整数部分和小数部分拼接起来的结果
  return sign + integerPart + decimalPart;
}

// label,value形式的数组取值
export function valueTransLabelObj(data = []) {
  if(!Array.isArray(data)) return data
  const obj = {}
  data.forEach(item => {
    obj[String(item.value)] = item.label
  })
  return obj
}
